
import { _decorator, Node, CCBoolean, Vec2, Component, UITransform, Enum } from 'cc';
import { DEBUG, DEV } from 'cc/env';
import { SuperContainer } from './SuperContainer';
import { NeighborHandler } from '../base/NeighborHandler';
import { SuperButton } from '../base/SuperButton';
import { DIR } from '../../../tools/Define';

const { ccclass, property, requireComponent } = _decorator;

const _tempVec2 = new Vec2();

/**
 * Predefined variables
 * Name = CursorScrollView
 * DateTime = Sat Sep 18 2021 10:47:12 GMT+0800 (中国标准时间)
 * Author = moxiangyouxi
 * FileBasename = CursorScrollView.ts
 * FileBasenameNoExtension = CursorScrollView
 * URL = db://assets/scripts/app/mediator/common/CursorScrollView.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */

/**
 * 自定义布局组件，管理非列表的容器
 */
@ccclass('LayoutViewHelper')
@requireComponent(NeighborHandler)
export class LayoutViewHelper extends SuperContainer {

    @property({ tooltip: DEV && "内容父节点" })
    @property(Node)
    content: Node = null;

    @property([SuperButton])
    itemViews: SuperButton[] = [];

    @property({ tooltip: DEV && "键盘激活时时的默认选中项" })
    @property(SuperButton)
    defaultSelectedItemView: SuperButton = null;

    @property({ tooltip: DEV && "内容父节点的集合" })
    @property([Node])
    childParents: Node[] = [];

    onLoad() {

        for (let index = 0; index < this.childParents.length; index++) {
            const element = this.childParents[index];
            this.initWithContent(element)
        }
        if (this.content) {
            this.initWithContent(this.content)
        }
        for (let i = 0; i < this.itemViews.length; i++) {
            let itemView = this.itemViews[i];
            itemView.check.isChecked = false;
            itemView.sHoverCallback = this.onItemHoverCallback.bind(this);
            itemView.sSelectedCallback = this.onItemSelectedCallback.bind(this);
            // itemView.node.getComponent(ContainerView)?.setKeyboardActive(false);
            this.listener?.onItemAdd(itemView);
        }
    }

    initWithContent(content: Node) {
        let children = content.children;
        for (let i = 0; i < children.length; i++) {
            let itemView = children[i].getComponent(SuperButton);
            if (itemView) {
                this.itemViews.push(itemView);
            } else {
                // LogUtils.error("缺少SuperButton组件", children[i].name)
            }
        }
    }

    public getItemViews(): SuperButton[] {
        return this.itemViews;
    }

    getIndex(sb: SuperButton) {
        return this.itemViews.indexOf(sb)
    }

    addItemView(node: Node) {
        let itemView: SuperButton = node.getComponent(SuperButton);
        if (itemView) {
            itemView.sHoverCallback = this.onItemHoverCallback.bind(this);
            itemView.sSelectedCallback = this.onItemSelectedCallback.bind(this);
            this.itemViews.push(itemView);
            this.listener?.onItemAdd(itemView);
        } else {
            console.error('节点上没有挂SuperButton')
        }
    }

    removeItemView(node: Node) {
        let itemView: SuperButton = node.getComponent(SuperButton)
        let index = this.itemViews.indexOf(itemView)
        if (index >= 0) {
            this.itemViews.splice(index, 1)
            this.listener?.onItemRemove(itemView)
        }
    }

    removeAll() {
        this.itemViews.length = 0;
        this.defaultSelectedItemView = null;
        this.selectedItemView = null;
    }

    findDefaultItem() {
        if (!this.defaultSelectedItemView) {
            let df = null;
            for (let index = 0; index < this.itemViews.length; index++) {
                const element = this.itemViews[index];
                if (element.node.active == true) {
                    df = element;
                    break;
                }
            }
            if (df) {
                this.defaultSelectedItemView = df
            } else {
                console.log('没有可以作为默认选项的item')
            }
        }
    }

    public selectedDefaultItem() {

        if (this.defaultSelectedItemView) {
            if (this.selectedItemView) {
                this.selectedItemView.onFocusEvent()
            } else {
                this.defaultSelectedItemView.onClickForContainer();
            }
            // this._selectedItemView = this.defaultSelectedItemView;
        } else {

        }
    }

    public setDefaultSelectedItemView(itemView: SuperButton) {
        this.defaultSelectedItemView = itemView;
    }

    public getDefaultSelectedItemView(): SuperButton {
        return this.defaultSelectedItemView;
    }

    clearContents() {
        this.itemViews.length = 0;
    }


    public moveUp() {
        return this.next(DIR.UP);
    }

    public moveDown() {
        return this.next(DIR.DOWN);
    }

    public moveLeft() {
        return this.next(DIR.LEFT);
    }

    public moveRight() {

        return this.next(DIR.RIGHT);
    }

    protected next(dir: DIR): boolean {
        if (!this._selectedItemView) {
            this.findDefaultItem()
            this.selectedDefaultItem();
            return true;
        }
        // console.log(' this.itemViews ', this.itemViews)
        let item = this._selectedItemView.getComponent(NeighborHandler).getNext(dir, this.getContents())
        // console.log(' item ', item)
        if (item) {
            item.getComponent(SuperButton).onClickForContainer();
            return true;
        }
        return false;
    }


    getContents(): Component[] {
        return this.itemViews;
    }

}


